package com.iot.ic.modules.dao.impl;

import com.iot.ic.common.dao.BaseDaoImpl;
import com.iot.ic.modules.dao.PatientCaseDao;
import com.iot.ic.modules.model.PatientCase;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("patientCaseDao")
public class PatientCaseDaoImpl extends BaseDaoImpl implements PatientCaseDao {
    @Override
    public boolean diagnoCase(Integer id, Integer drId, String caseLeave, String diagnoNote) throws Exception {
        StringBuffer sql = new StringBuffer();
        sql.append(" update t_patient_case as t set t.diagno_time=now(),diagno_note=?1,case_level=?2,dr_id=?3 ,case_note =?5,treat_time=?6 where t.id =?4");
        EntityManager em = this.getEntiyManager();
        em.getTransaction().begin();
        try {
            Query query = em.createNativeQuery(sql.toString());
            query.setParameter(1, diagnoNote);
            query.setParameter(2, caseLeave);
            query.setParameter(3, drId);
            query.setParameter(4, id);
            query.setParameter(5, getCaseLevelNote(caseLeave));
            query.setParameter(6, getNoticeTime(caseLeave));
            int i = query.executeUpdate();
            em.getTransaction().commit();
            return i > 0;
        } catch (Exception e) {
            em.getTransaction().rollback();
            throw e;
        } finally {
            em.close();
        }
    }


    @Override
    public PatientCase findById(Integer id) throws Exception {
        EntityManager em = super.getEntiyManager();
        Query query = em.createNativeQuery(" select t.* from t_patient_case t where  t.id=?1", PatientCase.class);
        query.setParameter(1, id);
        List list = query.getResultList();
        Object outO;
        if (list == null || list.size() == 0) {
            outO = null;
        } else {
            outO = list.get(0);
        }
        em.close();
        return (PatientCase) outO;
    }

    @Override
    public List<PatientCase> findCase(Integer id, Integer userId, Integer drId, String caseLevel, Integer type) throws Exception {
        StringBuffer sql = new StringBuffer();
        sql.append(" select t.* from t_patient_case t where  1=1");
        if (id != null) {
            sql.append(" and id=" + id);
        }
        if (userId != null) {
            sql.append(" and user_Id=" + userId);
        }
        if (drId != null) {
            sql.append(" and dr_Id=" + drId);
        }
        if (caseLevel != null) {
            sql.append(" and case_Level=" + caseLevel);
        }
        if (type != null) {
            if (type == 1) {
                sql.append(" and case_level is null ");
            } else {
                sql.append(" and case_level is not null ");
            }
        }
        EntityManager em = super.getEntiyManager();
        Query query = em.createNativeQuery(sql.toString(),PatientCase.class);
        List list = query.getResultList();
        em.close();
        return  list;
    }

    @Override
    public List<PatientCase> findNoticeCase() throws Exception {
        StringBuffer sql = new StringBuffer();
        sql.append(" select t.* from t_patient_case t where dr_id is not NULL  and notice_count<5 and treat_time<now() and TIMESTAMPDIFF(hour,treat_time,now())>12 ");
        EntityManager em = super.getEntiyManager();
        Query query = em.createNativeQuery(sql.toString(),PatientCase.class);
        List list = query.getResultList();
        em.close();
        return list;
    }

    public String getCaseLevelNote(String caseLevel) {
        if (caseLevel.equals("a")) {
            return "牙齿未见异常，建议每2个月常规复查";
        } else if (caseLevel.equals("b")) {
            return "牙齿可见轻微异常，先观察，暂不处理，建议2个月后线上复查";
        } else if(caseLevel.equals("c")){
            return "牙齿异常，建议尽早去医院就医";
        } else{
        	return "口腔图清晰的情况需要客户重新上传图片";
        }
    }

    public String getNoticeTime(String caseLevel) {
        long time = new Date().getTime();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (caseLevel.equals("a")) {
            time = time + 60 * 24 * 3600000l;
        } else if (caseLevel.equals("b")) {
            time = time + 30 * 24 * 3600000l;
        } else if (caseLevel.equals("d")) {
        	time = time + 30 * 24 * 3600000l;
        } else {
            time = time + 1800000l;
        }
        String time1 = sf.format(new Date(time));
        return time1;
    }
}
